<template>
  <div>
    <!-- 定价子任务页面     --------------------------------------------------------- -->

    <span class="formTitle"><i class="vertical-bar"></i>{{ title }}</span>
    <el-form
      :model="searchForm"
      ref="subRecordRef"
      label-width="100px"
      label-position="right"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="任务名称：" prop="taskName">
            <el-input
              v-model="searchForm.taskName"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="定价编号：" prop="pricingCode">
            <el-input
              v-model="searchForm.pricingCode"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="开始日期：" prop="taskSd">
            <el-input
              v-model="searchForm.taskSd"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="1">
          <el-form-item label="结束日期：" prop="taskEd">
            <el-input
              v-model="searchForm.taskEd"
              maxlength="32"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="container">
        <el-col style="margin-right:10px;" class="content-left">
          <upper-template :searchIcon="false" :configIcon="false">
            <!-- 操作按钮区 -->
            <template slot="buttonArea">
              <el-button @click="toBlock()">引擎任务</el-button>
              <el-button @click="toRecord()">执行记录</el-button>
              <el-button @click="goBack()">返回</el-button>
            </template>
          </upper-template>
        </el-col>
      </el-row>
      <el-table
        ref="subRecordModelTable"
        :data="subRecordModelData"
        style="width: 100%"
        @row-click="authorizeModelFor"
      >
        <el-table-column
          prop="pricingCode"
          label="定价编号"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="subTaskCode"
          label="子任务编号"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="dataDate"
          label="数据日期"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="startTm"
          label="开始时间"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="endTm"
          label="结束时间"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="运行状态"
          :formatter="r => $getLabel(r.status, statusOption)"
          :show-overflow-tooltip="true"
        ></el-table-column>
      </el-table>
      <!-- 分页插件 -->
      <pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.page"
        :page-size="searchForm.size"
        @refresh="toSubRecord"
        :total="total"
      ></pagination>
    </el-form>
  </div>
</template>

<script>
import { getDict } from "@/api/getDict";
import { pricingTask } from "@/api/index";
import moment from "moment";
export default {
  props: {
    
    //选中的值
    baseData: {
      type: Object,
      default: () => ({})
    },
    title: String
  },
  data() {
    return {
      positionOptions: {},
      statusOption: {},
      //定价子任务表单数据
      subRecordModelData: [],
      //定价子任务界面上方数据
      searchForm: {
        taskCode: this.baseData.taskCode,
        taskName: this.baseData.taskName,
        pricingCode: this.baseData.pricingCode,
        taskSd: this.baseData.taskSd,
        taskEd: this.baseData.taskEd,
        page: 1,
        size: 15
      },
      total:0,
      tableData: [],
      textData: [
        { prop: "pricingCode", label: "定价编号", disabled: true },
        { prop: "dataDate", label: "数据日期", disabled: true },
        { prop: "sceneCode", label: "情景编号", disabled: true },
        { prop: "sceneVersion", label: "情景版本", disabled: true },
        { prop: "startTm", label: "开始时间", disabled: true },
        { prop: "endTm", label: "结束时间", disabled: true },
        { prop: "successNum", label: "成功记录", disabled: true },
        { prop: "failNum", label: "失败记录", disabled: true },
        { prop: "status", label: "执行记录", disabled: true }
      ],
      checkList: []
    };
  },
  watch: {
    baseData: function(newVal, oldVal) {
      if (newVal != oldVal) {
        this.searchForm = { ...newVal, page: 1, size: 15 };
      } else {
      }
    }
  },
  components: {},
  created() {
    this.toSubRecord();
    this.getstatus();
  },
  computed: {},
  mounted() {},
  methods: {
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.toSubRecord();
      console.log(val, "每页数");
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.toSubRecord();
      console.log(val, "当前页");
    },
    formatRunStatus(row, column) {
      if (row.status == "INIT") {
        return "初始化";
      } else if (row.status == "PREPARING") {
        return "准备中";
      } else if (row.status == "RUNNING") {
        return "运行中";
      } else if (row.status == "FINISH") {
        return "已完成";
      } else if (row.status == "ERROR") {
        return "错误";
      } else if (row.status == "SENDBACK") {
        return "已退回";
      } else if (row.status == "RETRY") {
        return "再次尝试中";
      } else {
        return "";
      }
    }, 
    goBack() {
      this.$emit("backIndex", "");
    },
    getstatus(data) {
      this.$getDict("ftp_pricing_task_status").then(res => {
        if (res.code == 200) {
          this.statusOption = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    toBlock(){
        // 跳转
        console.log(this.searchForm,"引擎传值")
      this.$emit("toEngineTask", this.searchForm);
    },
    toRecord() {
      // 跳转
      this.$emit("toExcuteNotes", "");
    },
    toSubRecord() {
      // let selection = this.$refs.multipleTable.selection;
      // if (selection.length != 1) {
      //   return this.$message.warning("请选择一条数据");
      // }
      let data = { pricingCode: this.searchForm.pricingCode };
      pricingTask
        .toBlockSceneTask('"' + this.searchForm.pricingCode + '"')
        .then(res => {
          console.log(res, "跳转子任务的时候chaxundaodeshujv==============");
          if (res.code == 200) {
            this.searchForm.taskName = res.datas.task.taskName;
            this.searchForm.pricingCode = res.datas.pricingRecord.pricingCode;
            this.searchForm.dataDate = res.datas.pricingRecord.dataDate;
          }
          console.log(this.searchForm, "searchForm");
        });
      let pagedata = {
        pricingCode: this.searchForm.pricingCode,
        page: this.searchForm.page,
        size: this.searchForm.size
      };
      pricingTask.findAllSubRecord(pagedata).then(res => {
        console.log(res, "跳转子任务的时候chaxundaodeshujv==============");
        if (res.code == 200) {
          this.subRecordModelData = res.datas.rows.map(v => {
            let data = v;
            data.dataDate = data.dataDate
              ? moment(data.dataDate).format("yyyy-MM-DD")
              : "-";
            return data;
          });
          this.total = res.datas.total
        }
      });
    },
    //点击行选中
    authorizeModelFor(row) {
      if (this.$refs.subRecordModelTable) {
        this.$refs.subRecordModelTable.clearSelection();
      }
      this.$refs.subRecordModelTable.toggleRowSelection(row, true);
    },
    rowClick(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
    }
  }
};
</script>
